package com.riekoff.particlecubeworld.editor.verlegung;

import java.io.File;

import cc.creativecomputing.CCApp;
import cc.creativecomputing.CCApplicationManager;
import cc.creativecomputing.control.CCControl;
import cc.creativecomputing.io.CCIOUtil;

import com.riekoff.particlecubeworld.editor.ATilePattern;

public class AVerlegungEditor extends CCApp {
	
	private ATilePattern _myPattern = new ATilePattern(1,1);
	
	private AVerlegungTextField _myTextField;
	
	@CCControl(name = "space", min = 10, max = 100)
	private float _cSpace = 10;

	@Override
	public void setup() {
		addControls("app", "app", this);
		_myTextField = new AVerlegungTextField();
		_myTextField.setVisible(true);
	}

	@Override
	public void update(final float theDeltaTime) {
		reloadPatter();
	}
	
	long myLast = 0;
	
	private void reloadPatter(){
		_myPattern.loadPattern(_myTextField.text());
	}

	@Override
	public void draw() {
		g.clear();
		if(_myPattern == null)return;
		if(_myPattern.width == 0 )return;
		if(_myPattern.height == 0 )return;
		
		g.pushMatrix();
		g.translate(-width/2, height/2);
		g.scale(1,-1);
		
		float resX = width / _cSpace;
		float resY = height / _cSpace;
		
		for(int x = 0; x < resX; x++){
			for(int y = 0; y < resY; y++){
				int patternX = x % _myPattern.width;
				int patternY = y % _myPattern.height;
//				CCLog.info(x + " : " + y + " : " + patternX + " : " + patternY);
				g.color(255);
				if(_myPattern.horizontals[patternX][patternY]){
					g.line(x * _cSpace, (y + 1) * _cSpace, (x + 1) * _cSpace, (y + 1) * _cSpace);
				}
				if(_myPattern.verticals[patternX][patternY]){
					g.line((x + 1) * _cSpace, y * _cSpace, (x + 1) * _cSpace, (y + 1) * _cSpace);
				}
//				if(_myPattern.colors != null){
//					CCColor myColor = _myPattern.colors[_myPattern.tileindices[patternX][patternY]];
//					g.color(0);
//					g.rect(x * _cSpace, (y) * _cSpace,_cSpace,_cSpace);
//				}
			}
		}
		
		g.popMatrix();
	}

	public static void main(String[] args) {
		CCApplicationManager myManager = new CCApplicationManager(AVerlegungEditor.class);
		myManager.settings().size(500, 500);
		myManager.start();
	}
}
